﻿using Developer.Code;
using Developer.Service;
using Super.Framework.Common;
using Super.Framework.Common.Secure;
using Super.Framework.Common.Web;
using Super.Framework.EF;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using static Super.Framework.MVC.CommonController;

namespace Developer.Web.Controllers
{
    public class IndexController : BaseController
    {
        // GET: Index/Home
        public ActionResult Home()
        {
            var pageIndex = Query.Value("page").ToInt(1);
            int pageCount;
            int itemCount;
            PageSize = 6;
            var AllBooks = BookService.GetAll().Where(a=>a.IsAbort == false).ToList();
            var list = AllBooks.OrderByDescending(a=>a.CreateTime)
                .Page(out itemCount, out pageCount, pageIndex: pageIndex, pageSize: PageSize);
            ViewBag.pageCount = pageCount;
            ViewBag.itemCount = itemCount;
            return View(list);
        }

        [HttpPost]
        public JsonResult GetUserHead()
        {
            var userId = Query.Value("UID").ToInt();
            var user = UserService.GetAll().Find(userId);
            Hashtable result = new Hashtable();

            if (user.WeChats.Count() == 0)
            {
                if(user.HeadImg == null)
                {
                    result.Add("head", "/Content/UI/images/head.jpg");
                }
                else
                {
                    result.Add("head", user.HeadImg);
                }
            }
            else
            {
                result.Add("head", user.WeChats.FirstOrDefault().HeadImg);
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult SearchResult()
        {
            var key = Query.Value("key");
            var books = BookService.GetAll().Where(a => a.IsAbort == false &&( a.Name.Contains(key) || a.ISBN.Contains(key)));
            return View(books);
        }
        
    }
}